package com.sysu.homework.service.Impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.sysu.homework.DAO.GroupDAO;
import com.sysu.homework.DAO.StudentDAO;
import com.sysu.homework.model.Group;
import com.sysu.homework.model.Student;
import com.sysu.homework.service.StudentService;

@Component("studentService")
public class StudentServiceImpl implements StudentService{

	private StudentDAO studentDAO;
	private GroupDAO groupDAO;
	
	@Override
	public boolean isExist(int id) {
		
		return getStudentDAO().isExist(id);
	}

	@Override
	public int addStudent(Student student) {
		if(student!=null)
			return getStudentDAO().save(student);
		return -1;
	}

	@Resource
	public void setStudentDAO(StudentDAO studentDAO) {
		this.studentDAO = studentDAO;
	}

	public StudentDAO getStudentDAO() {
		return studentDAO;
	}

	@Override
	public boolean isValid(int studentid, String password) {
		
		return studentDAO.isValid(studentid, password);
	}

	@Override
	public int getCourseId(int studentId) {
		if (studentId != 0)
			return studentDAO.getCourseId(studentId);
		return 0;
	}

	@Override
	public int createGroup(Group group) {
		if (group != null) {
			return studentDAO.saveGroup(group);
		}
		return -1;
	}

	@Override
	public Student getStudentById(int id) {
		if (id!=0) 
			return studentDAO.getStudentById(id);
		return null;
	}

	@Override
	public int updateStudent(Student student) {
		if (student != null)
			return studentDAO.updateStudent(student);
		return 0;
	}

	@Override
	public List<Group> getAllGroups() {

		return studentDAO.getAllGroups();
	}

	@Override
	public int addToGroup(int studentId, int groupId) {
		Student student = studentDAO.getStudentById(studentId);
		Group group = groupDAO.getGroupById(groupId);
		if (group.getIsLock()==true)
			return -1;
		student.setGroup(group);
		return studentDAO.updateStudent(student);
	}

	@Resource
	public void setGroupDAO(GroupDAO groupDAO) {
		this.groupDAO = groupDAO;
	}

	public GroupDAO getGroupDAO() {
		return groupDAO;
	}

	@Override
	public int removeFromGroup(int studentId, int groupId) {
		Student student = studentDAO.getStudentById(studentId);
		Group group = groupDAO.getGroupById(groupId);
		if (group.getIsLock()==true )
			return -1;
		if (group.getStudents().size()>1 && group.getGroupLeader().equals(student))
			return -1;
		student.setGroup(null);
		int size = group.getStudents().size();
		if (size <=1) {
			groupDAO.removeGroup(groupId);
		}
			
		return studentDAO.updateStudent(student);
	}

	@Override
	public int addLockToGroup(int groupId) {
		if (groupId > 0){
			Group group = groupDAO.getGroupById(groupId);
			group.setIsLock(true);
			return groupDAO.setLockToGroup(group);
		}
		return -1;
	}

	@Override
	public int removeLockToGroup(int groupId) {
		if (groupId > 0){
			Group group = groupDAO.getGroupById(groupId);
			group.setIsLock(false);
			return groupDAO.setLockToGroup(group);
		}
		return -1;
	}

	@Override
	public int getGroupId(int studentid) {
		if (studentid > 0) {
			return studentDAO.getGroupId(studentid);
		}
		return 0;
	}

}
